﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Threading;
public partial class Admin_LoaiCD : System.Web.UI.Page
{
    LoaiCD CD = new LoaiCD();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["user"] != null)
            {
                GridView1.DataSource = CD.LayLoaiCD();
                GridView1.DataBind();
               
            }
            else
                Response.Redirect("DangNhap.aspx");
        }
        
    }
  
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        int  ma = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);

        TextBox txtten = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
        string ten = txtten.Text;
        
        TextBox txtgia = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
        float  gia = float.Parse(txtgia.Text);

        TextBox txttienphat = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
        float  tienphat = float.Parse(txttienphat.Text);

        CD.SuaLoaiCD(ma, ten, gia, tienphat);
        GridView1.EditIndex = -1;
        GridView1.DataSource = CD.LayLoaiCD();
        GridView1.DataBind();
       
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("ThemLoaiCD.aspx");
    }


    
   
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = CD.LayLoaiCD();
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        //GridView1.DataSource = CD.TimLoaiCD(TextBox1.Text, float.Parse(TextBox2.Text), float.Parse(TextBox3.Text));
        GridView1.DataSource = CD.TimLoaiCD(TextBox1.Text,TextBox2.Text, TextBox3.Text);
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = CD.LayLoaiCD();
        GridView1.DataBind();
        
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = CD.LayLoaiCD();
        GridView1.DataBind();
    }

   
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int mal = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
        int macd = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
        try
        {
            CD.Xoa(mal);
            Label1.Text = "da xoa";

        }

        catch (Exception ex)
        {

            Label1.Text = ex.Message;
        }
        
        GridView1.DataSource = CD.LayLoaiCD();
        GridView1.DataBind();
        

       
    }
   
    protected void Button3_Click(object sender, EventArgs e)
    {
        Thread.Sleep(2000);
        try
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("cb");

                if (chb.Checked==true)
                {
                   int ma=Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);
                   CD.Xoa(ma);
                   Label1.Text = "da xoa";
                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;

        }

        GridView1.DataSource = CD.LayLoaiCD();
        GridView1.DataBind();
    }
}
